//
//  ViewController.m
//  Test
//
//  Created by mac on 16/3/9.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
#import "Message.h"
#import "SQLiteManager.h"

@interface ViewController () <UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITextField* textfield;
@property (weak, nonatomic) IBOutlet UITableView* tableView;

@property (assign, nonatomic) BOOL isPressReturn;

@property (strong, nonatomic) NSTimer *timer;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.textfield.delegate = self;

    self.tableView.dataSource = self;
    
    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextFieldTextDidEndEditingNotification object:nil];

    //为文本框添加信号源
    [[self.textfield.rac_textSignal filter:^BOOL(id value) {
        NSString* text = value;
        return text.length > 3;
    }] subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];

    //设置文本框的颜色
    RACSignal* validTextSignal = [self.textfield.rac_textSignal map:^id(NSString* text) {
        return @([self isValidTextField:text]);
    }];
    [[validTextSignal map:^id(NSNumber* textValid) {
        return [textValid boolValue] ? [UIColor clearColor] : [UIColor yellowColor];
    }] subscribeNext:^(UIColor* color) {
        self.textfield.backgroundColor = color;
    }];
    
    [self loadData];
    
    [self setupTimer];
}

- (BOOL)isValidTextField:(NSString*)text
{
    return self.textfield.text.length > 3;
}

//使用信号源
- (void)TextFieldDemo
{
    [[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UITextFieldTextDidEndEditingNotification object:nil] takeUntil:[self rac_willDeallocSignal]] subscribeNext:^(id x) {
        NSLog(@"x:%@,%@", x, [x class]);
    }];
}

- (void)textDidChange
{
    NSLog(@"%@", self.textfield.text);
}

//启动return键，并结束编辑
- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
    [self.textfield endEditing:YES];
    self.isPressReturn = YES;
    return YES;
}

//通知的销毁
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//加载数据
- (void)loadData
{
    NSString *sql = @"SELECT name FROM T_Message";
    
    NSArray *array = [[SQLiteManager sharedManager] execRecordSet:sql];
    NSLog(@"%@",array);
}

//设置时钟
- (void)setupTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(addMessage) userInfo:nil repeats:YES];
    
    //调整优先级
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

}

//创建消息
- (void)addMessage
{
    Message *message = [[Message alloc] init];
    message.name = [NSString stringWithFormat:@"测试 %zd",arc4random_uniform(1000)];
    
    //将数据添加到数据库中
    NSDictionary *dict = @{@"name":message.name};
    [[SQLiteManager sharedManager] execInsert:dict];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    static NSString* identifier = @"cell";

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    cell.textLabel.text = @"1111";

    return cell;
}

@end
